package com.element.music.service.impl;

import com.element.music.controller.vo.CollectionIdVO;
import com.element.music.repository.entity.collection.CollectionEntity;
import com.element.music.repository.repository.CollectionRepository;
import com.element.music.service.CollectionService;
import com.element.music.service.converter.CollectionConverter;
import com.element.music.service.dto.CollectionDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述：
 * <p>
 * User: nft musician   developer
 * Date: 2022/1/10
 */

@Service
@Slf4j
@Transactional
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionRepository collectionRepository;

    @PersistenceContext
    private EntityManager em;

    public EntityManager getEntityManager() {
        return em;
    }

    @Override
    public List<CollectionDTO> getCollectionList() {
        List<CollectionEntity> all = collectionRepository.findAll();
        return all.stream().map(CollectionConverter::entity2Dto).collect(Collectors.toList());
    }

    @Override
    public CollectionDTO getCollectionById(Long collectionId) {
        CollectionEntity one = collectionRepository.getOne(collectionId);
        return CollectionConverter.entity2Dto(one);
    }

    @Override
    public CollectionDTO getCollectionByContractAddress(String contractAddress) {
        CollectionEntity collectionEntityByContractAddress = collectionRepository
                .findCollectionEntityByContractAddress(contractAddress);
        return CollectionConverter.entity2Dto(collectionEntityByContractAddress);
    }

    @Override
    public Boolean delCollectionByContractAddress(String contractAddress) {
        return collectionRepository.deleteByContractAddress(contractAddress);
    }

    @Override
    public CollectionIdVO addCollection(String contractAddress) {
        CollectionEntity collectionEntity = new CollectionEntity();
        collectionEntity.setContractAddress(contractAddress);
        CollectionEntity save = collectionRepository.save(collectionEntity);
        return CollectionIdVO.of().setCollectionDetailId(save.getId());
    }

    @Override
    public Boolean updateCollectionDescription(Long collectionId, String description) {
        collectionRepository.updateCollectionByDescription(collectionId, description);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateCollectionWebsite(Long collectionId, String website) {
        collectionRepository.updateCollectionByWebsite(collectionId, website);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateCollectionTwitter(Long collectionId, String twitter) {
        collectionRepository.updateCollectionByTwitter(collectionId, twitter);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateCollectionOpenSea(Long collectionId, String openSea) {
        collectionRepository.updateCollectionByOpenSea(collectionId, openSea);
        return Boolean.TRUE;
    }

}
